﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
using System.Text.RegularExpressions;

namespace Utility
{
    public class FavorReader
    {
        public static string TranslateFavorToXml(string favorFolderPath)
        {
            if (favorFolderPath == null)
            {
                throw new ArgumentNullException("xmlFilePath");
            }

            if (!Directory.Exists(favorFolderPath))
            {
                throw new ArgumentException("The argument must be an existing dictionary");
            }

            using (FileStream fs = File.OpenWrite("a.xml"))
            {
                XmlWriter write = XmlWriter.Create(fs);
                write.WriteStartElement("root");
                WriteCurrentDictionaryToXml(write, favorFolderPath);
                write.WriteEndElement();
                write.Flush();
                write.Close();
            }

            return null;
        }

        static void WriteCurrentDictionaryToXml(XmlWriter writer, string path)
        {
            writer.WriteStartElement("dictionary");
            writer.WriteAttributeString("path", getFileName(path));
            
            foreach (var directory in Directory.GetDirectories(path))
            {
                WriteCurrentDictionaryToXml(writer, directory);
            }

            foreach (var file in Directory.GetFiles(path))
            {
                writer.WriteStartElement("UrlFile");
                WriteUrlContent(writer, file);
                writer.WriteValue(getFileName(file));
                writer.WriteEndElement();
            }
            writer.WriteEndElement();

        }

        private static string getFileName(string filePath)
        {
            FileInfo fi = new FileInfo(filePath);
            return fi.Name;
        }

        private static void WriteUrlContent(XmlWriter writer, string filePath)
        {
            Regex titleRegex = new Regex(@"(?<=^\[).+(?=\]$)");
            Regex contentRegex = new Regex(@"^\w+=.+$");
            Regex contentRegexName = new Regex(@"^\w+(?==.+$)");
            Regex contentRegexValue = new Regex(@"(?<=^\w+=).+$");
            using (StreamReader sr = new StreamReader(filePath))
            {
                bool opened = false;
                while (!sr.EndOfStream)
                {
                    string line = sr.ReadLine();
                    Match matchTitle = titleRegex.Match(line);
                    if (matchTitle.Success)
                    {
                        if (opened)
                        {
                            writer.WriteEndElement();
                            opened = false;
                        }

                        writer.WriteStartElement(matchTitle.Value);
                        opened = true;
                    }
                    else if (contentRegex.IsMatch(line))
                    {
                        writer.WriteAttributeString(contentRegexName.Match(line).Value, contentRegexValue.Match(line).Value);
                    }
                    else
                    {
                        continue;
                    }
                }

                if (opened)
                {
                    writer.WriteEndElement();
                }
            }
        }
    }
}
